package com.thon.controller.rest;

import com.thon.commons.persistence.Page;
import com.thon.controller.util.BaseController;
import com.thon.entity.paper.Paper;
import com.thon.entity.system.Attachment;
import com.thon.service.paper.PaperService;
import com.thon.service.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Chen on 2016/2/22.
 */
@Controller
@RequestMapping("/api/paper")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class PaperRestController extends BaseController {
    
    @Autowired
    private PaperService paperService;

    @RequestMapping(value="/save", method = RequestMethod.POST)
    public ResponseEntity<?> apiPaperSave(@Valid Paper paper) {

        paperService.savePaper(paper);

        return new ResponseEntity(true, HttpStatus.OK);
    }
    
    @RequestMapping(value="/del", method = RequestMethod.POST)
    public ResponseEntity<?> apiPaperDel(@RequestParam(value = "id",required = true) int id) {
            Paper paper = paperService.getPaper(id);
            paperService.delPaper(paper);
            return new ResponseEntity(true, HttpStatus.OK);

    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<?> PaperList(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                       @RequestParam(value = "s", defaultValue = "30") int pageSize,
                                       Paper paper) {

        Page<Paper> page = new Page<Paper>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page = paperService.findPapers(page,paper);
        return new ResponseEntity(page, HttpStatus.OK);
    }

}
